package com.tjsinfo.tjsoa.common;



import java.util.*;
import java.sql.Timestamp;
import java.text.*;

public class DayFormat {
	public static final String FormatDayToChinaTime24H = "yyyy��MM��dd��";

	/**
	 * ��yyyy-mm-ddתΪyyyy-m-d
	 * 
	 * @param day
	 *            ��������֮�������
	 * @return Date:��������֮�������������;
	 */
	public static String getYMDDays(String day){
		String dayTime=day;
	    if(String.valueOf(day.charAt(8)).equals("0"))dayTime=day.substring(0,8)+day.substring(9);
	    if(String.valueOf(day.charAt(5)).equals("0"))dayTime=dayTime.substring(0,5)+dayTime.substring(6);
	    return dayTime;
    }
	
	
	/**
	 * }�����ڼ������
	 * 
	 * @param days
	 *            ��������֮�������
	 * @return Date:��������֮�������������;
	 */
	public static int getIntervalDays(Date startday,Date endday){        
        if(startday.after(endday)){
            Date cal=startday;
            startday=endday;
            endday=cal;
        }        
        long sl=startday.getTime();
        long el=endday.getTime();       
        long ei=el-sl;           
        return (int)(ei/(1000*60*60*24));
    }

	
	
	/**
	 * ȡ�þ������ڶ����죨��������֮�������죩
	 * 
	 * @param days
	 *            ��������֮�������
	 * @return Date:��������֮�������������;
	 */
	public static Date getDate(int days) {
		Date dateresult = new Date();
		try {
			DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.FULL);
			// Create our Gregorian Calendar.
			GregorianCalendar cal = new GregorianCalendar();

			// Set the date and time of our calendar
			// to the system&s date and time
			cal.setTime(new Date());
			cal.add(GregorianCalendar.DAY_OF_MONTH, days);
			dateresult = cal.getTime();
		} catch (Exception e) {
			
		}
		return dateresult;
	}

	/**
	 * @��ݵ�ǰ���ڼ���n��������
	 * @return String
	 */
	public static Date afterNDay(Date date, int n) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		c.setTime(date);
		c.add(Calendar.DATE, n);
		Date d2 = c.getTime();
		// String s=df.format(d2);
		return d2;
	}
	
	/**
	 * @��ݵ�ǰ���ڼ���n��������
	 * @return String
	 */
	public static String afterDay(Date date, int n) {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		// String s=df.format(d2);
		return df.toString();
	}

	/**
	 * @��ȡ��ǰʱ�������ڼ����������ա�
	 * @return String
	 */
	public static String getDayInWeek() {
		Date today = new Date(System.currentTimeMillis());
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
		return simpleDateFormat.format(today);
	}
	/**
	 * 
	 * @����ַ�ʱ��4������ڼ���(SimpleDateFormat("yyyy-MM-dd"))
	 * @return
	 */
public static String getDayInWeek(String mydatestring) {
	 SimpleDateFormat   dateFormat   =   new   SimpleDateFormat("yyyy-MM-dd");   
     Date   date   =   null; 
     String   showDate   =""; 
     try   {   
             date   =   dateFormat.parse(mydatestring);   
     }   catch   (ParseException   e)   {   

     }   
     Calendar   cd   =   Calendar.getInstance();   
     cd.setTime(date);   
     int   mydate   =   cd.get(Calendar.DAY_OF_WEEK);   
    
     switch   (mydate)   {   
     case   1:   
             showDate   =   "������";   
             break;   
     case   2:   
             showDate   =   "����һ";   
             break;   
     case   3:   
             showDate   =   "���ڶ�";   
             break;   
     case   4:   
             showDate   =   "������";   
             break;   
     case   5:   
             showDate   =   "������";   
             break;   
     case   6:   
             showDate   =   "������";   
             break;   
     default:   
             showDate   =   "������";   
             break;   
     }   

	return showDate;
	
}
	/**
	 * @�������ʱ�䣬��ȡ��ǰʱ�������ڼ����������ա�
	 * @return String
	 */
	public static String getDayInWeek(Date date) {
		if (date == null)
			return "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("E");
		return simpleDateFormat.format(date);
	}

	/**
	 * @��ȡ��ǰʱ��������µ���
	 * @return String
	 */
	public static String getDayInMonth() {
		Date today = new Date(System.currentTimeMillis());
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d");
		return simpleDateFormat.format(today);
	}

	/**
	 * @������ڣ���ȡ��ǰʱ��������µ���
	 * @return String
	 */
	public static String getDayInMonth(Date date) {
		if (date == null)
			return "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("d");
		return simpleDateFormat.format(date);
	}

	/**
	 * @����ڱ��굱�У���ȡ�·ݸ�ʽ"M"
	 * @return String
	 */
	public static String getMonthInYear() {
		Date today = new Date(System.currentTimeMillis());
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M");
		return simpleDateFormat.format(today);
	}

	/**
	 * @����ڱ��굱�У���ȡ�·ݸ�ʽ"MM"
	 * @return String
	 */
	public static String getMonthInYear2() {
		java.util.Date today = new Date(System.currentTimeMillis());
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
		return simpleDateFormat.format(today);
	}

	/**
	 * @������ڣ���ȡ�·ݸ�ʽ"MM"
	 * @return String
	 */
	public static String getMonthInYear2(String formatDate) {
		java.util.Date day = getDate(formatDate, "yyyy-MM-dd");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM");
		return simpleDateFormat.format(day);
	}

	/**
	 * @��ȡ��ǰʱ����꣬��ʽ"yyyy"
	 * @return String
	 */
	public static String getYear() {
		java.util.Date today = new java.util.Date(System.currentTimeMillis());
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
		return simpleDateFormat.format(today);
	}

	/**
	 * @���ʱ�䣬��ȡ�꣬��ʽ"yyyy"
	 * @return String
	 */
	public static String getYear(String formatDate) {
		java.util.Date day = getDate(formatDate, "yyyy-MM-dd");
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy");
		return simpleDateFormat.format(day);
	}

	/**
	 * @��������Լ����ڸ�ʽ����ȡ�����ַ���
	 * @return String
	 */
	public static String getFormatDate(java.util.Date thisDate, String format) {
		if (thisDate == null)
			return "";
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		return simpleDateFormat.format(thisDate);
	}

	/**
	 * @��������Լ����ڸ�ʽ����ȡ����
	 * @return String
	 */
	public static java.util.Date getDate(String date, String format) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
		try {
			return simpleDateFormat.parse(date);
		} catch (ParseException ex) {
			return null;
		}
	}

	/**
	 * @����ϵͳ��ǰ�µĵ�һ�����ʾ������
	 * @param monthDate
	 *            Date
	 * @return Date
	 */
	public static java.util.Date getFirstDateMonth() {
		try {
			String date_s = getYear() + "-" + getMonthInYear2() + "-01";
			return getDate(date_s, "yyyy-MM-dd");
		} catch (Exception er) {
			er.printStackTrace();
		}
		return null;
	}

	/**
	 * @����ϵͳ��ǰ�µĵ�һ�����ʾ������
	 * @param monthDate
	 *            Date
	 * @return Date
	 */
	public static java.util.Date getFirstDateMonth(String formatDate) {
		try {
			String date_s = getYear(formatDate) + "-"
					+ getMonthInYear2(formatDate) + "-01";
			return getDate(date_s, "yyyy-MM-dd");
		} catch (Exception er) {
			er.printStackTrace();
		}
		return null;
	}

	/**
	 * @����ϵͳ��ǰ�µĵ�һ�����ʾ������
	 * @param monthDate
	 *            Date
	 * @return Date
	 */
	public static String getFirstDateMonthToString() {
		try {
			return getYear() + "-" + getMonthInYear2() + "-01";
		} catch (Exception er) {
			er.printStackTrace();
		}
		return null;
	}

	/**
	 * @����ϵͳ��ǰ�µĵ�һ�����ʾ������
	 * @param monthDate
	 *            Date
	 * @return Date
	 */
	public static String getFirstDateMonthToString(String formatDate) {
		try {
			return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
					+ "-01";
		} catch (Exception er) {
			er.printStackTrace();
		}
		return null;
	}

	/**
	 * @����ϵͳ��ǰ�µ����һ�����ʾ������
	 * @return Date
	 */
	public static java.util.Date getLastDateMonth() {
		try {
			int lastday = getDaySumInTheMonth();
			String date_s;
			if (lastday < 10)
				date_s = getYear() + "-" + getMonthInYear2() + "-0" + lastday;
			else
				date_s = getYear() + "-" + getMonthInYear2() + "-" + lastday;
			return getDate(date_s, "yyyy-MM-d");
		} catch (Exception er) {
			er.printStackTrace();
		}
		return null;
	}

	/**
	 * @����ϵͳ��ǰ�µ����һ�����ʾ������
	 * @return Date
	 */
	public static String getLastDateMonthToString(String formatDate) {
		try {
			int lastday = getDaySumInTheMonth(formatDate);
			if (lastday < 10)
				return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
						+ "-0" + lastday;
			else
				return getYear(formatDate) + "-" + getMonthInYear2(formatDate)
						+ "-" + lastday;
		} catch (Exception er) {
			er.printStackTrace();
		}
		return null;
	}

	/**
	 * @���ʱ�䣬�������һ�����ʾ������
	 * @param formatDate
	 *            String
	 * @return Date
	 */
	public static java.util.Date getLastDateMonth(String formatDate) {
		try {
			int lastday = getDaySumInTheMonth(formatDate);
			String month = getMonthInYear2(formatDate);
			String year = getYear(formatDate);
			String date_s;
			if (lastday < 10)
				date_s = year + "-" + month + "-0" + lastday;
			else
				date_s = year + "-" + month + "-" + lastday;

			System.out.println(date_s);

			return getDate(date_s, "yyyy-MM-d");
		} catch (Exception er) {
			er.printStackTrace();
		}
		return null;
	}

	/**
	 * @�������µ�����
	 * @return int
	 */
	public static int getDaySumInTheMonth() {
		String month = DayFormat.getMonthInYear();
		if (month.equalsIgnoreCase("12"))
			return 31;
		int mon = Integer.parseInt(month);
		java.util.Date date = DayFormat.getDate(DayFormat.getYear() + "-"
				+ (mon + 1) + "-" + "1", "yyyy-MM-d");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, -1);
		return Integer.parseInt(new SimpleDateFormat("dd")
				.format(cal.getTime()));
	}

	/**
	 * @������������·ݵ�����
	 * @return int
	 */
	public static int getDaySumInTheMonth(java.util.Date date) {
		String month = new SimpleDateFormat("MM").format(date);
		if (month.equalsIgnoreCase("12"))
			return 31;
		int mon = Integer.parseInt(month);
		java.util.Date date2 = DayFormat.getDate(new SimpleDateFormat("yyyy")
				.format(date)
				+ "-" + (mon + 1) + "-" + "1", "yyyy-MM-d");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date2);
		cal.add(Calendar.DATE, -1);
		return Integer.parseInt(new SimpleDateFormat("dd")
				.format(cal.getTime()));
	}

	/**
	 * @������������·ݵ�����
	 * @return int
	 */
	public static int getDaySumInTheMonth(String formatDate) {
		String month = DayFormat.getMonthInYear2(formatDate);
		if (month.equalsIgnoreCase("12"))
			return 31;
		int mon = Integer.parseInt(month);
		java.util.Date date = DayFormat.getDate(DayFormat.getYear(formatDate)
				+ "-" + (mon + 1) + "-" + "1", "yyyy-MM-d");
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, -1);
		return Integer.parseInt(new SimpleDateFormat("dd")
				.format(cal.getTime()));
	}

	/**
	 * @�����ڸ�ʽת�����ַ��ʽ,��ʽΪ'yyyy-MM-dd'
	 * @param date
	 *            Date
	 * @return String
	 */
	public static String getFormatDate(java.util.Date date) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		return simpleDateFormat.format(date);
	}

	public final static String FormatDay1 = "yyyy-MM-dd";
	public final static String FormatDay2 = "yyyy��MM��dd��";
	public final static String FormatDay3 = "yyyy��MM��dd�� HH:mm:ss";
	public final static String FormatDay4 = "yyyy-MM-dd HH:mm";

	public static final String Format24Datetime1 = "yyyy-MM-dd HH:mm:ss";// 24

	public static final String Format12Datetime1 = "yyyy-MM-dd hh:mm:ss";// 12

	/**
	 * ��ȡ��ǰʱ��
	 * 
	 * @return
	 */
	public static String getToday() {
		return getToday(FormatDay1);
	}

	public static java.sql.Timestamp getCurrentTimestamp() {
		return new Timestamp(System.currentTimeMillis());
	}

	public static java.util.Date getCurrentUtilDate() {
		return new java.util.Date(System.currentTimeMillis());
	}

	public static String getToday(String formatDay) {
		java.util.Date today = new java.util.Date();
		SimpleDateFormat df = new SimpleDateFormat(formatDay);
		return df.format(today).toString();
	}

	public static String get24DateTime() {
		return get24DateTime(Format24Datetime1);
	}

	public static String get24DateTime(String format24DateTime) {
		java.util.Date today = new java.util.Date();
		SimpleDateFormat df = new SimpleDateFormat(format24DateTime);
		return df.format(today).toString();
	}

	public static String get12DateTime() {
		return get12DateTime(Format12Datetime1);
	}

	public static String get12DateTime(String format12DateTime) {
		java.util.Date today = new java.util.Date();
		SimpleDateFormat df = new SimpleDateFormat(format12DateTime);
		return df.format(today).toString();
	}

	public static String get24DateTime(Timestamp timestamp) {
		SimpleDateFormat df = new SimpleDateFormat(Format24Datetime1);
		return df.format(timestamp).toString();
	}

	public static String get12DateTime(Timestamp timestamp) {
		SimpleDateFormat df = new SimpleDateFormat(Format12Datetime1);
		return df.format(timestamp).toString();
	}

	// STRING������
	public static java.sql.Date stringToDate(String dateStr) {
		return java.sql.Date.valueOf(dateStr);
	}

	public static java.sql.Timestamp stringToTimestamp(String timestampStr) {
		if (timestampStr == null || timestampStr.length() < 1)
			return null;
		return java.sql.Timestamp.valueOf(timestampStr);
	}

	public static java.sql.Timestamp stringToTimestamp2(String dateStr) {
		if (dateStr == null || dateStr.length() < 1)
			return null;
		return java.sql.Timestamp.valueOf(dateStr + " 00:00:00.000000000");
	}

	public static java.sql.Timestamp stringToTimestamp3(String dateStr) {
		if (dateStr == null || dateStr.length() < 1)
			return null;
		return java.sql.Timestamp.valueOf(dateStr + ":00.000000000");
	}

	public static java.sql.Time stringToTime(String timeStr) {
		return java.sql.Time.valueOf(timeStr);
	}

	// ���ڵ�STRING
	public static String dateToString(java.sql.Date datee) {
		return datee.toString();
	}

	public static String timestampToString(java.sql.Timestamp timestampe) {
		return timestampe.toString();
	}

	public static String timestampToStringForFormat(Timestamp timestamp,
			String format) {
		if (timestamp == null)
			return "";
		SimpleDateFormat df = new SimpleDateFormat(format);
		return df.format(timestamp).toString();
	}

	public static String getTimestampToDateTime15Len(
			java.sql.Timestamp timestampe) {
		if (timestampe == null || timestampe.toString().length() < 1)
			return "";
		return timestampe.toString().substring(0, 16);
	}

	public static String timeToString(java.sql.Time timee) {
		return timee.toString();
	}

	public static java.sql.Timestamp StringToTimestamp(String dateString)
			throws ParseException {
		java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat(
				FormatDay1);
		formatter.setLenient(false);
		return new java.sql.Timestamp((formatter.parse(dateString).getTime()));
	}

	public static java.sql.Timestamp StringToTimestampAll(String dateString)
			throws ParseException {
		return java.sql.Timestamp.valueOf(dateString);
	}
	//������յ����� 
    public   static   String   getSunday(Date   date){   
        Calendar   c   =   Calendar.getInstance();   
        c.setTime(date);   
        c.set(Calendar.DAY_OF_WEEK,Calendar.SUNDAY);   
        return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
      }    
      //�����һ������   
      public   static   String   getMonday(Date   date){   
        Calendar   c   =   Calendar.getInstance();   
        c.setTime(date);   
        c.set(Calendar.DAY_OF_WEEK,Calendar.MONDAY);   
        return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
      }   
      //����ܶ�������
      public   static   String   getTuesday(Date   date){   
          Calendar   c   =   Calendar.getInstance();   
          c.setTime(date);   
          c.set(Calendar.DAY_OF_WEEK,Calendar.TUESDAY);   
          return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
        } 
      //������������ 
      public   static   String   getWednesday(Date   date){   
          Calendar   c   =   Calendar.getInstance();   
          c.setTime(date);   
          c.set(Calendar.DAY_OF_WEEK,Calendar.WEDNESDAY);   
          return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
        } 
      //������ĵ�����
      public   static   String   getThursday(Date   date){   
          Calendar   c   =   Calendar.getInstance();   
          c.setTime(date);   
          c.set(Calendar.DAY_OF_WEEK,Calendar.THURSDAY);   
          return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
        } 
      //������������   
      public   static   String   getFriday(Date   date){   
        Calendar   c   =   Calendar.getInstance();   
        c.setTime(date);   
        c.set(Calendar.DAY_OF_WEEK,Calendar.FRIDAY);         
        return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());       
      }   
      //������������ 
      public   static   String   getSaturday(Date   date){   
          Calendar   c   =   Calendar.getInstance();   
          c.setTime(date);   
          c.set(Calendar.DAY_OF_WEEK,Calendar.SATURDAY);   
          return   new   SimpleDateFormat("yyyy-MM-dd").format(c.getTime());   
        } 
 
	public static void main(String args[]) {
		try {
			String d = "2007-05-15 19:23:48.703";
			
			java.sql.Timestamp timestampe = java.sql.Timestamp.valueOf(d);
			
		} catch (Exception er) {
			er.printStackTrace();
		}
	}
	
	/**
	  * hanzhuo 
	  * 按指定日期单位计算两个日期间的间隔
	  * 
	  * @param timeInterval
	  * @param date1
	  * @param date2
	  * @return
	  */
	 public static long dateDiff(String timeInterval, Date date1, Date date2) {
	  if (timeInterval.equals("year")) {
	   Calendar calendar = Calendar.getInstance();
	   calendar.setTime(date1);
	   int time = calendar.get(Calendar.YEAR);
	   calendar.setTime(date2);
	   return time - calendar.get(Calendar.YEAR);
	  }

	  if (timeInterval.equals("quarter")) {
	   Calendar calendar = Calendar.getInstance();
	   calendar.setTime(date1);
	   int time = calendar.get(Calendar.YEAR) * 4;
	   calendar.setTime(date2);
	   time -= calendar.get(Calendar.YEAR) * 4;
	   calendar.setTime(date1);
	   time += calendar.get(Calendar.MONTH) / 4;
	   calendar.setTime(date2);
	   return time - calendar.get(Calendar.MONTH) / 4;
	  }

	  if (timeInterval.equals("month")) {
	   Calendar calendar = Calendar.getInstance();
	   calendar.setTime(date1);
	   int time = calendar.get(Calendar.YEAR) * 12;
	   calendar.setTime(date2);
	   time -= calendar.get(Calendar.YEAR) * 12;
	   calendar.setTime(date1);
	   time += calendar.get(Calendar.MONTH);
	   calendar.setTime(date2);
	   return time - calendar.get(Calendar.MONTH);
	  }

	  if (timeInterval.equals("week")) {
	   Calendar calendar = Calendar.getInstance();
	   calendar.setTime(date1);
	   int time = calendar.get(Calendar.YEAR) * 52;
	   calendar.setTime(date2);
	   time -= calendar.get(Calendar.YEAR) * 52;
	   calendar.setTime(date1);
	   time += calendar.get(Calendar.WEEK_OF_YEAR);
	   calendar.setTime(date2);
	   return time - calendar.get(Calendar.WEEK_OF_YEAR);
	  }

	  if (timeInterval.equals("day")) {
	   long time = date1.getTime() / 1000 / 60 / 60 / 24;
	   return time - date2.getTime() / 1000 / 60 / 60 / 24;
	  }

	  if (timeInterval.equals("hour")) {
	   long time = date1.getTime() / 1000 / 60 / 60;
	   return time - date2.getTime() / 1000 / 60 / 60;
	  }

	  if (timeInterval.equals("minute")) {
	   long time = date1.getTime() / 1000 / 60;
	   return time - date2.getTime() / 1000 / 60;
	  }

	  if (timeInterval.equals("second")) {
	   long time = date1.getTime() / 1000;
	   return time - date2.getTime() / 1000;
	  }

	  return date1.getTime() - date2.getTime();
	 }
}
